<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../../dbcom/CouponCom.php");
require_once(dirname(__FILE__) . "/../../dbcom/ClosureCom.php");
require_once(dirname(__FILE__) . "/../../dbcom/MerchantCom.php");
require_once(dirname(__FILE__) . "/../../dbcom/UserCom.php");
require_once(dirname(__FILE__) . "/../../dbcom/PayOrderCom.php");

$payordercom = new PayOrderCom();
$merchantcom = new MerchantCom();
$couponcom = new CouponCom();
$closurecom = new ClosureCom();
$usercom = new UserCom();

if (isset($_REQUEST['str'])) {
    $str = $_REQUEST['str'];
} else {
    $str = "";
}

$limitnum = 15;
if (empty($_REQUEST['p']) || !isset($_REQUEST['p'])) {
    $page = 1;
    $minlimit = 0;
} else {
    $page = $_REQUEST['p'];
    if ($page > 1) {
        $minlimit = $limitnum * ($page - 1);
    } else {
        $minlimit = 0;
    }
}

$res = array();

$orderlist = $payordercom->getAllCou($minlimit, $limitnum);
$number = $payordercom->getCouNumber();
if (!empty($orderlist)) {
    foreach ($orderlist as $k => $v) {
        $res[$k]['id'] = $v['id'];
        $userinfo = $usercom->getUserInfo($v['user_id']);
        $res[$k]['user'] = $userinfo['name'];
        $merchantinfo = $merchantcom->getMerchant($v['merchant_id']);
        $res[$k]['merchant'] = $merchantinfo['name'];
        $couponinfo = $couponcom->getCoupon($v['coupon_id']);
        $res[$k]['code'] = $couponinfo['code'];
        $res[$k]['total_fee'] = $couponinfo['total_fee'];
        if($couponinfo['type'] == 1){
            $res[$k]['type'] = "扣除券";
        }else{
            $res[$k]['type'] = "满减券";
        }
        $res[$k]['time'] = date("Y-m-d H:i:s",$v['time']);
    }
}

$rem = $number % $limitnum;
if ($rem == 0) {
    $p = (int) ($number / $limitnum);
} else {
    $p = (int) ($number / $limitnum + 1);
}
?>
<!DOCTYPE html>
<html>

    <head>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="keywords" content="">
        <meta name="description" content="">
        <link href="../public/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
        <link href="../public/css/font-awesome.css?v=4.4.0" rel="stylesheet">

        <link href="../public/css/animate.css" rel="stylesheet">
        <link href="../public/css/style.css?v=4.1.0" rel="stylesheet">

        <link href="../public/css/paging.css?v=1.0.2" rel="stylesheet">
        <link href="../public/css/showproject.css?v=1.0.0" rel="stylesheet">
        <link href="../public/css/jquery.timepicker.css" rel="stylesheet">
        <link href="../public/webupload/webuploader.css?v=1.0.1" rel="stylesheet">
        <link href="../public/webupload/style.css?v=1.0.1" rel="stylesheet">
    </head>

    <body class="gray-bg">

        <div class="wrapper wrapper-content animated fadeInUp">
            <div class="row">
                <div class="col-sm-12">

                    <div class="ibox">
                        <div class="ibox-title">
                            <h5>财务报告</h5>
                            <div class="ibox-tools">
                                <a class="collapse-link" style="color:#1c2b36;" title="导出" data-toggle="modal" data-target="#myModal1" id="download"><i class="fa fa-cloud-download"></i></a>
                            </div>
                        </div>
                        <div class="ibox-content">
                            <div style="width:100%;margin-bottom:10px;">
<!--                                <div class="input-group">
                                    <input class="input-sm form-control" id="searchstr" value="<?//= $str ?>" type="text" placeholder="请输入商户名称或商户联系方式"/>
                                    <span class="input-group-btn"> 
                                        <button class="btn btn-sm btn-primary" id="search">搜索</button>
                                    </span>
                                </div>-->
                            </div>

                            <div class="table-responsive">
                                <table class="table table-hover">
                                    <thead>
                                        <tr >
                                            <th  >
                                                <span><input name="select" type="checkbox" id="checkall"/></span>
                                            </th>
                                            <th  >
                                                <span>序号</span>
                                            </th>
                                            <th  >
                                                <span>优惠券自编号</span>
                                            </th>
                                            <th  >
                                                <span>类型</span>
                                            </th>
                                            <th  >
                                                <span>价格</span>
                                            </th>
                                            <th  >
                                                <span>时间</span>
                                            </th>
                                            <th  >
                                                <span>用户</span>
                                            </th>
                                            <th  >
                                                <span>核销商户</span>
                                            </th>
                                            <th >
                                                <span>操作</span>
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        foreach ($res as $k => $v) {
                                            ?>
                                            <tr id="list<?= $v['id'] ?>" data-id="<?= $v['id'] ?>">
                                                <td >
                                                    <input name="option" data-id="<?= $v['id'] ?>"  type="checkbox"/>
                                                </td>
                                                <td >
                                                    <span><?= $k+1; ?></span>
                                                </td>
                                                <td >
                                                    <span><?= $v['code'] ?></span>
                                                </td>
                                                <td >
                                                    <span><?= $v['type'] ?></span>
                                                </td>
                                                <td >
                                                    <span><?= $v['total_fee'] ?></span>
                                                </td>
                                                <td >
                                                    <span><?= $v['time'] ?></span>
                                                </td>
                                                <td >
                                                    <span><?= $v['user'] ?></span>
                                                </td>
                                                <td >
                                                    <span><?= $v['merchant'] ?></span>
                                                </td>
                                                <td >
                                                    
<!--                                                    <a data-id="<?= $v['id'] ?>" class="btn btn-white btn-sm btn-det"><i class="fa fa-folder"></i> 删除 </a>-->
                                                </td>
                                            </tr>
                                        <?php } ?>
                                    </tbody>
                                </table>
                                <div class="box" id="box"></div>
                            </div>

                        </div>
                    </div>
                </div>
            </div>

        </div>
        
        <div class="modal fade" id="myModal1" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="width:800px;">
            <div class="modal-content">
                <input type="hidden" id="routeid"/>
                <div class="modal-header">

                    <button type="button" class="close" id="close1" data-dismiss="modal" aria-hidden="true">
                        <i class="fa fa-times"></i>
                    </button>
                    <h4 class="modal-title" id="myModalLabel" style="text-align: center;">财务导出</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label>选择要导出的日期</label>
                        <input type="text" placeholder="" id="time" class="form-control" >
                    </div>
                </div>
                <div class="modal-footer" style="text-align: center;">
                    <button type="button" id="gotodown" class="btn btn-primary btn-rounded btn-block" style="width:70%;margin:0 auto;">导&nbsp;&nbsp;出</button>
                </div>
                <div style="width:100%;height:40px;"></div>
            </div><!-- /.modal-content -->

        </div><!-- /.modal -->
    </div>

        <div style="z-index:100000;display: none;position: fixed;margin:auto;width: 309px;height: 210px;top:0;right:0;bottom:0;left:0;" id="loading">
            <div class="ibox ">
                <div class="ibox-title">
                    <h5>正在加载</h5>
                </div>
                <div class="ibox-content">
                    <div class="spiner-example">
                        <div class="sk-spinner sk-spinner-wave">
                            <div class="sk-rect1"></div>
                            <div class="sk-rect2"></div>
                            <div class="sk-rect3"></div>
                            <div class="sk-rect4"></div>
                            <div class="sk-rect5"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- 全局js -->
        <script src="../public/js/jquery.min.js?v=2.1.4"></script>
        <script src="../public/js/bootstrap.min.js?v=3.3.6"></script>
        <script src="../public/js/plugins/layer/layer.min.js"></script>

        <!-- 自定义js -->
        <script src="../public/js/paging.js?v=1.0.1"></script>
        <script src="../public/webupload/webuploader.nolog.js?v=1.0.0"></script>
        <script src="../public/js/common.js?v=1.0.4"></script>
        <script src="../public/js/jquery.datetimepicker.full.min.js?lang=zh"></script>
        <script src="../public/js/ajaxFileUpload.js?v=1.0.1"></script>
        <script>
            function isCardNo(card)
            {
                // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X  
                var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
                if (reg.test(card) === false)
                {
                    return  false;
                }
                return true;
            }

            $(document).ready(function () {

                $('#loading-example-btn').click(function () {
                    btn = $(this);
                    simpleLoad(btn, true)

                    // Ajax example
                    //                $.ajax().always(function () {
                    //                    simpleLoad($(this), false)
                    //                });

                    simpleLoad(btn, false)
                });
                $("#time").datetimepicker({
                    lang: 'ch',
                    format: 'Y-m-d',
                    timepicker: false
                });
                $("#outtime").datetimepicker({
                    lang: 'ch',
                    format: 'Y-m-d',
                    timepicker: false
                });
                $("#oldintime").datetimepicker({
                    lang: 'ch',
                    format: 'Y-m-d',
                    timepicker: false
                });
                $("#oldouttime").datetimepicker({
                    lang: 'ch',
                    format: 'Y-m-d',
                    timepicker: false
                });
                var setTotalCount = <?= $number ?>;
                $('#box').paging({
                    initPageNo: <?= $page ?>, // 初始页码
                    totalPages: <?= $p ?>, //总页数
                    totalCount: '合计' + setTotalCount + '条数据', // 条目总数
                    slideSpeed: 600, // 缓动速度。单位毫秒
                    jump: true, //是否支持跳转
                    callback: function (page) { // 回调函数
                        console.log(page);
                    }
                });
            });

            $("#search").click(function () {
                var str = $("#searchstr").val();
                var datastr = "?str=" + str;
                var url = window.location.href;
                var arr = url.split('?');
                var furl = arr[0];
                //console.log(furl);
                //console.log(furl + "?str=" + str);
                window.location.href = furl + "?str=" + str;
            });
            
            $("#gotodown").click(function () {
                var time = $("#time").val();
                if (time == "") {
                    layer.msg('请填写需要导出的时间', {
                        time: 2000, //20s后自动关闭
                        btn: ['OK']
                    });
                    return;
                }
                $("#loading").fadeIn();
                var datastr = "cont=2&time=" + time;
                $.ajax({
                    url: "/../../controller/excelcont.php",
                    type: "POST",
                    data: datastr,
                    success: function (e) {
                        $("#loading").fadeOut();
                        var result = eval("(" + e + ")");
                        if (result.code == 0) {
                            $("#close1").trigger("click");
                            window.open(result.data);
                        } else {
                            layer.msg('操作失败请刷新重试', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            return;
                        }
                        return;
                    }
                });
            });
            
            $("#savenew").click(function () {
                var name = $("#name").val();
                var mobile = $("#mobile").val();
                if (name == "" || mobile == "") {
                    layer.msg('请填写必填项', {
                        time: 2000, //20s后自动关闭
                        btn: ['OK']
                    });
                    return;
                }
                var address = $("#address").val();
                var datastr = "cont=1&name=" + name + "&mobile=" + mobile + "&address=" + address;
                $.ajax({
                    url: "/../../controller/merchant.php",
                    type: "POST",
                    data: datastr,
                    success: function (e) {
                        var res = eval("(" + e + ")");
                        if (res.code == 0) {
                            layer.msg("成功", {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            }, function () {
                                window.location.reload();
                            });
                        } else {
                            layer.msg('失败,请刷新重试', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                        }
                        return;
                    }
                });
            });
            $(".btn-et").click(function () {
                var id = $(this).data("id");
                $("#merchantid").val(id);
                var datastr = "cont=2&id=" + id;
                $.ajax({
                    url: "/../../controller/merchant.php",
                    type: "POST",
                    data: datastr,
                    success: function (e) {
                        var res = eval("(" + e + ")");
                        if (res.code == 0) {
                            $("#oldname").val(res.data.name);
                            $("#oldaddress").val(res.data.address);
                            $("#oldmobile").val(res.data.mobile);
                        } else {
                            layer.msg('失败,请刷新重试', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                        }
                        return;
                    }
                });
            });
            $("#saveold").click(function () {
                var name = $("#oldname").val();
                var mobile = $("#oldmobile").val();
                var address = $("#oldaddress").val();
                var id = $("#merchantid").val();
                var datastr = "cont=3&name=" + name + "&mobile=" + mobile + "&address=" + address + "&id=" + id;
                $.ajax({
                    url: "/../../controller/merchant.php",
                    type: "POST",
                    data: datastr,
                    success: function (e) {
                        var res = eval("(" + e + ")");
                        if (res.code == 0) {
                            layer.msg("成功", {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            }, function () {
                                window.location.reload();
                            });
                        } else {
                            layer.msg('失败,请刷新重试', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                        }
                        return;
                    }
                });
            });
            $(".btn-det").click(function () {
                layer.confirm("确认要删除？", {
                    title: "信息",
                    btn: ["确定", "取消"]
                }, function (index) {
                    var id = $(this).data("id");
                    var datastr = "cont=4&id=" + id;
                    $.ajax({
                        url: "/../../controller/merchant.php",
                        type: "POST",
                        data: datastr,
                        success: function (e) {
                            var res = eval("(" + e + ")");
                            if (res.code == 0) {
                                layer.msg("成功", {
                                    time: 2000, //20s后自动关闭
                                    btn: ['OK']
                                }, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg('失败,请刷新重试', {
                                    time: 2000, //20s后自动关闭
                                    btn: ['OK']
                                });
                            }
                            return;
                        }
                    });
                });
            });
            $(".btn-delall").click(function () {
                layer.confirm("确认要删除？", {
                    title: "信息",
                    btn: ["确定", "取消"]
                }, function (index) {
                    var ids = getcheckids();
                    if (ids == "") {
                        layer.msg('未选择任何记录', {
                            time: 2000, //20s后自动关闭
                            btn: ['OK']
                        });
                        return;
                    }
                    var datastr = "cont=5&ids=" + ids;
                    $.ajax({
                        url: "/../../controller/merchant.php",
                        type: "POST",
                        data: datastr,
                        success: function (e) {
                            var res = eval("(" + e + ")");
                            if (res.code == 0) {
                                layer.msg("成功", {
                                    time: 2000, //20s后自动关闭
                                    btn: ['OK']
                                }, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg('失败,请刷新重试', {
                                    time: 2000, //20s后自动关闭
                                    btn: ['OK']
                                });
                                return;
                            }
                            return;
                        }
                    });
                });
                return;
            });
        </script>

    </body>
</html>